package gui.bf3;

import javax.swing.BoundedRangeModel;

import transparent.TransparentSlider;
import file.SettingsDefinition;
import file.ValueObject;

public class BF3Slider extends TransparentSlider implements BF3Setting {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3714424820957109205L;
	protected SettingsDefinition settingsDefinition;

	public BF3Slider(SettingsDefinition settingsDefinition) {
		this.settingsDefinition = settingsDefinition;
	}

	public BF3Slider(SettingsDefinition settingsDefinition, int arg0) {
		super(arg0);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3Slider(SettingsDefinition settingsDefinition, BoundedRangeModel arg0) {
		super(arg0);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3Slider(SettingsDefinition settingsDefinition, int arg0, int arg1) {
		super(arg0, arg1);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3Slider(SettingsDefinition settingsDefinition, int arg0, int arg1, int arg2) {
		super(arg0, arg1, arg2);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3Slider(SettingsDefinition settingsDefinition, int arg0, int arg1, int arg2, int arg3) {
		super(arg0, arg1, arg2, arg3);
		this.settingsDefinition = settingsDefinition;
	}

	@Override
	public void updateValue() {

		int selectedValue = 50;

		ValueObject[] valuesObjects = settingsDefinition.getValueObjects();
		if (valuesObjects != null) {
			if (this.getMaximum() == 100 || this.getMaximum() == 200) {
				selectedValue = (int) (Float.parseFloat(valuesObjects[1].getValue()) * 100);
			} else if (this.getMaximum() == 180) {
				selectedValue = (int) (Float.parseFloat(valuesObjects[1].getValue()));
			} else {
				selectedValue = Integer.parseInt(valuesObjects[1].getValue());
			}
		}
		this.setValue(selectedValue);
		this.setToolTipText(String.valueOf(selectedValue));
	}

	@Override
	public void setValue(String value) {
		if (this.getMaximum() == 100 || this.getMaximum() == 200) { // is percentage

			value = String.valueOf(Float.parseFloat(value) / 100);

			while (value.length() < 8) {
				value = value + "0";
			}
			value = value.substring(0, 8);
		} else if (this.getMaximum() == 180) { // is fov

			value = String.valueOf(Float.parseFloat(value));

			while (value.length() < 9) {
				value = value + "0";
			}
			value = value.substring(0, 9);

		}
		ValueObject[] valueObjects = settingsDefinition.getValueObjects();
		if (valueObjects != null) {
			valueObjects[1].setValue(value);
			//System.out.println("Setting to:" + value);
		}

	}

}
